package com.baicaixiaozhan.springsecurityswaggerdemo.controller;

import com.baicaixiaozhan.springsecurityswaggerdemo.pojo.Person;
import com.baicaixiaozhan.springsecurityswaggerdemo.service.CustomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * DESC: 测试 controller 层
 *
 * @author : baicaixiaozhan
 * @since : 2020-11-04
 */
@Api(tags = "test demo")
@RestController
@RequestMapping("/person")
public class CustomController {

    private final CustomService customService;

    @Autowired
    public CustomController(CustomService customService) {
        this.customService = customService;
    }

    @ApiOperation("test api")
    @GetMapping(value = "/custom")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_DEVELOPMENT', 'ROLE_CONSUMER')")
    public String custom() {
        return "custom";
    }

    @ApiOperation("Search Person List")
    @GetMapping(value = "/list")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_DEVELOPMENT', 'ROLE_CONSUMER')")
    public List<Person> getPersonList() {
        return customService.getPersonList();
    }

    @ApiOperation("Search Person By Name")
    @ApiImplicitParam(name = "name", value = "NAME", required = true)
    @GetMapping(value = "/{name}")
    @PreAuthorize("hasAuthority('admin:read')")
    public Person getPersonByName(@PathVariable("name") String name) {
        return customService.getPersonByName(name);
    }


}